package com.weixing.mall.pay.channel.wx;


import com.weixing.mall.pay.channel.wx.sdk.IWXPayDomain;
import com.weixing.mall.pay.channel.wx.sdk.WXPay;
import com.weixing.mall.pay.channel.wx.sdk.WXPayConfig;
import com.weixing.mall.pay.channel.wx.sdk.WXPayUtil;
import com.weixing.mall.pay.properties.WxPayProperties;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class WxPayConfigImpl extends WXPayConfig {

    private static WxPayConfigImpl INSTANCE;

    private WxPaySet context;

    private String key;

    private WxTradeTypeEnum tradeType = WxTradeTypeEnum.JSAPI;

    private WxPayConfigImpl() {
    }

    public static WxPayConfigImpl getInstance() {

        return new WxPayConfigImpl();

//        return INSTANCE;
    }

    @Override
    public String getAppID() {
        String appId = null;
        switch (tradeType) {
            case H5:
            case JSAPI:
            case NATIVE:
                appId = context.getAppid();
                break;
            case XCX:
                appId = context.getAppletId();
                break;
            default:
                appId = context.getAppid();
                break;
        }
        return appId;
    }

    @Override
    public String getMchID() {
        return context.getMchid();
    }

    @Override
    public String getKey() {
        return key;

    }

    @Override
    public InputStream getCertStream() {
        return null;
    }

    @Override
    public IWXPayDomain getWXPayDomain() {
        return WXPayDomainSimpleImpl.instance();
    }

    @Override
    public boolean shouldAutoReport() {
        return false;
    }

    public WxPaySet getContext() {
        return context;
    }

    public void setContext(WxPaySet context) {
        this.context = context;
        if (context.isUseSandbox()) {
            key = getSandboxSignKey();
        } else {
            key = context.getKey();
        }
    }

    public WxTradeTypeEnum getTradeType() {
        return tradeType;
    }

    public void setTradeType(WxTradeTypeEnum tradeType) {
        this.tradeType = tradeType;
    }

    public String getSandboxSignKey() {
        try {
            WXPay wxPay = new WXPay(this);
            Map<String, String> params = new HashMap<String, String>();
            params.put("mch_id", this.getMchID());
            params.put("nonce_str", WXPayUtil.generateNonceStr());
            params.put("sign", WXPayUtil.generateSignature(params, this.context.getKey()));
            String strXML = wxPay.requestWithoutCert("/sandboxnew/pay/getsignkey",
                    params, this.getHttpConnectTimeoutMs(), this.getHttpReadTimeoutMs());

            Map<String, String> result = WXPayUtil.xmlToMap(strXML);
            System.out.println("retrieveSandboxSignKey:" + result);
            if ("SUCCESS".equals(result.get("return_code"))) {
                return result.get("sandbox_signkey");
            }
            return null;
        } catch (Exception e) {
            System.out.println("获取sandbox_signkey异常" + e.getMessage());
            return null;
        }
    }


}
